const { ccclass, property } = cc._decorator;

@ccclass
export default class EnemyControl extends cc.Component {
    @property(cc.AudioClip)
    BoomAudio: cc.AudioClip = null;
    @property(cc.Prefab)
    BulletPre: cc.Prefab = null;
    PointNode:cc.Node;
    //上下左右四个方位
    UP: cc.Vec2 = cc.v2(0, 1);
    Down: cc.Vec2 = cc.v2(0, -1);
    Left: cc.Vec2 = cc.v2(0, -1);
    Right: cc.Vec2 = cc.v2(0, -1);
    //坦克速度
    Speed: number = 1500;
    //刚体
    EnemyRig: cc.RigidBody;
    //射线长度
    RayLength: number = 10;

    onLoad() {
        cc.director.getPhysicsManager().enabled = true;
    }

    fire() {
        this.schedule(() => {
            //实例化预制体
            let Bullet = cc.instantiate(this.BulletPre)
            //设置父节点
            Bullet.setParent(cc.director.getScene());
            //角度设置一样
            Bullet.angle = this.node.angle;
            //转世界坐标
            let PointWorldTo = this.PointNode.convertToWorldSpaceAR(cc.v2(0, 0))
            Bullet.x = PointWorldTo.x
            Bullet.y = PointWorldTo.y
        },2)
    }

    start() {
        this.EnemyRig = this.node.getComponent(cc.RigidBody);
        this.PointNode = this.node.getChildByName('Point');
        //物理deg
        let ReyDeg = cc.PhysicsManager.DrawBits
        //| ReyDeg.e_aabbBit 
        // cc.director.getPhysicsManager().debugDrawFlags = ReyDeg.e_jointBit | ReyDeg.e_shapeBit
        this.ChangeDir();
        this.fire();
    }

    ChangeDir() {
        //随即四个方位
        let DirRand = Math.random() * 4;
        //取整
        let FloorDir = Math.floor(DirRand);
        //向上移动，改变角度
        if (FloorDir == 0) {
            this.node.angle = 0
        } else if (FloorDir == 1) {
            //向下移动，改变角度
            this.node.angle = 180;
        } else if (FloorDir == 2) {
            //向左移动，改变角度
            this.node.angle = 90;
        } else if (FloorDir == 3) {
            //向右移动，改变角度
            this.node.angle = 270;
        }

    }

    died() {
        //爆炸音效
        cc.audioEngine.playEffect(this.BoomAudio, false);
        this.node.destroy();
    }

    onBeginContact(concat, slef, other) {
        console.debug(other.tag)
        if (other.tag == 0) {
            this.died();
        } else if (other.tag == 4) {
            console.debug('撞墙了!')

        }
    }

    update(dt) {
        //当前角度转弧度
        let CurrentRad = cc.misc.degreesToRadians(this.node.angle);
        //弧度转向量算法
        let rotateV2 = cc.v2(0, -1).rotate(CurrentRad);
        //坦克移动
        this.EnemyRig.linearVelocity = cc.v2(rotateV2.x * this.Speed * dt, rotateV2.y * this.Speed * dt);
        //判断当前角度，给射线方向，碰到墙体后调用随即转向方法
        if (this.node.angle == 0) {
            console.debug('AI当前方向下')
            let TankRay = cc.director.getPhysicsManager().rayCast(this.node.getPosition(), cc.v2(this.node.x, this.node.y - this.RayLength), cc.RayCastType.Closest)
            if (TankRay.length > 0) {
                this.ChangeDir();
            }
        } else if (this.node.angle == 180) {
            console.debug('AI当前方向上')
            let TankRay = cc.director.getPhysicsManager().rayCast(this.node.getPosition(), cc.v2(this.node.x, this.node.y + this.RayLength), cc.RayCastType.Closest)
            if (TankRay.length > 0) {
                this.ChangeDir();
            }
        } else if (this.node.angle == 90) {
            console.debug('AI当前方向右')
            let TankRay = cc.director.getPhysicsManager().rayCast(this.node.getPosition(), cc.v2(this.node.x + this.RayLength, this.node.y), cc.RayCastType.Closest)
            if (TankRay.length > 0) {
                this.ChangeDir();
            }
        } else if (this.node.angle == 270) {
            console.debug('AI当前方向左')
            let TankRay = cc.director.getPhysicsManager().rayCast(this.node.getPosition(), cc.v2(this.node.x - this.RayLength, this.node.y), cc.RayCastType.Closest)
            if (TankRay.length > 0) {
                this.ChangeDir();
            }
        }

    }
}
